package com.zufe.test6;

public class Test3 {
    public static void main(String[] args) {
        Student[] arr = new Student[3];
        //2.创建学生对象并添加到数组当中
        Student stu1 = new Student(1, "ZhangSan", 23);
        Student stu2 = new Student(2, "LiSi", 24);
        Student stu3 = new Student(3, "WangSan", 25);

        arr[0] = stu1;
        arr[1] = stu2;
        arr[2] = stu3;
        int index = getIndex(arr, 2);
        if(index != -1) {
            arr[index].setAge(arr[index].getAge() + 1);
            printArr(arr);
        }else{
            System.out.println("id不存在");
        }
    }

    public static int getIndex(Student[] arr, int id){
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != null) {
                if (arr[i].getId() == id) {
                    return i;
                }
            }
        }
        return -1;
    }
    public static void printArr(Student[] arr) {
        for (Student student : arr) {
            if(student != null){
                System.out.println("id:" + student.getId() + " name:" + student.getName() + " age:" + student.getAge());
            }
        }
    }
}
